package com.nanhai.buddhist.college.module.collegebase.service.rewardpunishrecord;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.common.util.number.NumberUtils;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentroll.StudentRollDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.studentroll.StudentRollMapper;
import com.nanhai.buddhist.college.module.collegebase.enums.BooleanIntEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.rewardpunishrecord.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.rewardpunishrecord.RewardPunishRecordDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.collegebase.convert.rewardpunishrecord.RewardPunishRecordConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.rewardpunishrecord.RewardPunishRecordMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.*;

/**
 * 奖罚管理 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class RewardPunishRecordServiceImpl implements RewardPunishRecordService {

    @Resource
    private RewardPunishRecordMapper rewardPunishRecordMapper;

    @Resource
    private StudentRollMapper studentRollMapper;

    @Override
    public String createRewardPunishRecord(RewardPunishRecordCreateReqVO createReqVO) {
        // 插入
        RewardPunishRecordDO rewardPunishRecord = RewardPunishRecordConvert.INSTANCE.convert(createReqVO);
        rewardPunishRecord.setSerialNumber(NumberUtils.getCodeByServ("JF"));

        StudentRollDO studentRollDO = studentRollMapper.selectById(createReqVO.getStudentId());
        if(Objects.isNull(studentRollDO)){
            throw exception(STUDENT_ROLL_NOT_EXISTS);
        }
        rewardPunishRecord.setClassesId(studentRollDO.getClassesId());
        rewardPunishRecordMapper.insert(rewardPunishRecord);
        // 返回
        return rewardPunishRecord.getId();
    }

    @Override
    public void updateRewardPunishRecord(RewardPunishRecordUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRewardPunishRecordExists(updateReqVO.getId());
        // 更新
        RewardPunishRecordDO updateObj = RewardPunishRecordConvert.INSTANCE.convert(updateReqVO);
        rewardPunishRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRewardPunishRecord(String id) {
        // 校验存在
        this.validateRewardPunishRecordExists(id);
        // 删除
        rewardPunishRecordMapper.deleteById(id);
    }

    private void validateRewardPunishRecordExists(String id) {
        if (rewardPunishRecordMapper.selectById(id) == null) {
            throw exception(REWARD_PUNISH_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RewardPunishRecordDO getRewardPunishRecord(String id) {
        return rewardPunishRecordMapper.selectById(id);
    }

    @Override
    public List<RewardPunishRecordDO> getRewardPunishRecordList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : rewardPunishRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RewardPunishRecordDO> getRewardPunishRecordPage(RewardPunishRecordPageReqVO pageReqVO) {
        return rewardPunishRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RewardPunishRecordDO> getRewardPunishRecordList(RewardPunishRecordExportReqVO exportReqVO) {
        return rewardPunishRecordMapper.selectList(exportReqVO);
    }

    @Override
    public void cancelRewardPunishRecord(String id) {
        RewardPunishRecordDO rewardPunishRecordDO = rewardPunishRecordMapper.selectById(id);
        rewardPunishRecordDO.setCancelStatus(BooleanIntEnum.TRUE.getStatus());
        rewardPunishRecordMapper.updateById(rewardPunishRecordDO);
    }

}
